package com.ruoyi.foodworks.service;

import com.ruoyi.foodworks.domain.CommentReply;
import com.ruoyi.foodworks.domain.CommentStats;
import com.ruoyi.foodworks.domain.ReplyRequest;
import com.ruoyi.foodworks.mapper.CommentReplyMapper;
import com.ruoyi.foodworks.mapper.CommentStatsMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 评论回复Service业务层处理
 * 
 * @author ruoyi
 * @date 2024-01-01
 */
@Service
public class CommentReplyService {
    
    @Autowired
    private CommentReplyMapper replyMapper;
    
    @Autowired
    private CommentStatsMapper statsMapper;
    
    /**
     * 获取评论回复列表
     */
    public List<CommentReply> getRepliesByCommentId(Long commentId) {
        return replyMapper.selectRepliesWithUserInfo(commentId);
    }
    
    /**
     * 创建回复
     */
    @Transactional
    public CommentReply createReply(ReplyRequest request) {
        // 创建回复
        CommentReply reply = new CommentReply();
        reply.setCommentId(request.getCommentId());
        reply.setUserId(request.getUserId());
        reply.setContent(request.getContent());
        reply.setTargetUsername(request.getTargetUsername());
        
        replyMapper.insertCommentReply(reply);
        
        // 更新评论统计
        updateCommentStats(request.getCommentId());
        
        return reply;
    }
    
    /**
     * 删除回复
     */
    @Transactional
    public boolean deleteReply(Long replyId, Long userId) {
        CommentReply reply = replyMapper.selectCommentReplyById(replyId);
        if (reply == null || !reply.getUserId().equals(userId)) {
            return false;
        }
        
        replyMapper.deleteCommentReplyById(replyId);
        
        // 更新评论统计
        updateCommentStats(reply.getCommentId());
        
        return true;
    }
    
    /**
     * 更新评论统计
     */
    private void updateCommentStats(Long commentId) {
        // 计算实际回复数量
        Integer replyCount = replyMapper.selectReplyCountByCommentId(commentId);
        
        // 更新或插入统计记录
        CommentStats stats = statsMapper.selectByCommentId(commentId);
        if (stats == null) {
            stats = new CommentStats();
            stats.setCommentId(commentId);
            stats.setReplyCount(replyCount);
            statsMapper.insertCommentStats(stats);
        } else {
            stats.setReplyCount(replyCount);
            statsMapper.updateCommentStats(stats);
        }
    }
    
    /**
     * 获取评论统计信息
     */
    public Map<Long, CommentStats> getCommentStats(List<Long> commentIds) {
        if (commentIds == null || commentIds.isEmpty()) {
            return new HashMap<>();
        }
        
        List<CommentStats> statsList = statsMapper.selectByCommentIds(commentIds);
        Map<Long, CommentStats> statsMap = new HashMap<>();
        
        for (CommentStats stats : statsList) {
            statsMap.put(stats.getCommentId(), stats);
        }
        
        return statsMap;
    }
    
    /**
     * 获取单个评论的回复数量
     */
    public Integer getReplyCountByCommentId(Long commentId) {
        CommentStats stats = statsMapper.selectByCommentId(commentId);
        return stats != null ? stats.getReplyCount() : 0;
    }
    
    /**
     * 获取回复详情（包含用户信息）
     */
    public CommentReply getReplyDetail(Long replyId) {
        CommentReply reply = replyMapper.selectCommentReplyById(replyId);
        if (reply != null) {
            // 获取点赞数量
            Integer likeCount = replyMapper.selectReplyLikeCount(replyId);
            reply.setLikeCount(likeCount);
        }
        return reply;
    }
}
